package net.cs.rm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class PicTarget {
	HashMap<String,String> picMap;
	void buildMapByIndexFile(String fpath) throws Exception{
		String charsetName = "UTF-8"; 
		picMap = new HashMap<String,String>();
		//逐行处理,建立结构化数据
		File file = new File(fpath);
		if (file.isFile() && file.exists()) {
			InputStreamReader insReader = new InputStreamReader(
					new FileInputStream(file), charsetName);

			BufferedReader bufReader = new BufferedReader(insReader);

			String line = new String();
			int pos=0;
			while ((line = bufReader.readLine()) != null) {
				//System.out.println(line);
				pos++;
				if(line.trim().equals("")){
					continue;
				}
				picMap.put(getKeyByPath(line), line);
			}
			bufReader.close();
			insReader.close();
		}
		System.out.println("actual size:"+picMap.size());
	}
	String findPic(String name){
		Iterator<Entry<String,String>> iter = picMap.entrySet().iterator(); 
		String sort=null;
		String sort_uk=null;
		while (iter.hasNext()) { 
			Entry<String,String> entry = iter.next(); 
			if(match(name,entry.getKey())){
				return entry.getValue();
			}
		} 	
		return null;
	}
	//匹配规则
	boolean match(String src,String target){		
		return (target.indexOf(src)!=-1 || target.indexOf(src)!=-1);
	}
	String getKeyByPath(String path){
		int len = path.length();
		int ce=-1,cb=-1;
		for(int i=len-1;i>=0; i--){
			char ch = path.charAt(i);
			if(ce==-1 && Msyy.isChe(ch) && ch!='元'){
				ce=i;
			}
			if(ce!=-1 && !Msyy.isChe(ch)){
				cb=i;
				break;
			}
		}
		if(ce!=-1 && cb!=-1){
			return path.substring(cb+1, ce+1);
		}
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PicTarget pt = new PicTarget();
		try {
			pt.buildMapByIndexFile("/Users/chen4w/Documents/workspace/ipcm/WebRoot/st2/examples/rm/data/中华一万年1.txt");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
